package BizLogic;

import java.sql.SQLException;
import java.util.Random;
import java.util.ArrayList;

import model.LoginCredentials;
import model.Staff;
import model.StaffDetailsDTO;

import dao.DAOFactory;
import dao.LoginCredentialsDAO;
import dao.StaffDAO;
import dao.mysql.LoginCredentialsDAOImpl;
import dao.mysql.StaffNotFoundException;

public class LoginCredentialsManager {
	private LoginCredentialsDAO loginDAO;
	
	public LoginCredentialsManager(){
		
		loginDAO = DAOFactory.loadInstance().getLoginCredentialsDAO();
	}
	public ArrayList<String> getManagers() throws SQLException, BadDataException{
		
		ArrayList<String> managers= null;

				managers= loginDAO.findAllManagers();
				return managers;

		}
	public String getRole(String StaffID)
	{
		LoginCredentials lc=null;
		try {
			lc = loginDAO.findLoginCredentials(StaffID);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String role=lc.getRole();
		return role;
	}
	public ArrayList<StaffDetailsDTO> getStaffDetails()
	{
		ArrayList<StaffDetailsDTO> staff=new ArrayList<StaffDetailsDTO>();
			try {
				ArrayList<Staff> staf=new StaffManager().getAll();
				for(Staff f: staf)
				{
					StaffDetailsDTO dt=new StaffDetailsDTO();
					dt.setEmail(f.getEmail());
					dt.setName(f.getName());
					dt.setPhoneNumber(f.getPhoneNumber());
					dt.setReportTo(f.getReportTo());
					dt.setStaffID(f.getStaffID());
					dt.setRole(getRole(f.getStaffID()));
					staff.add(dt);
					
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		return staff;
	}
	public void addRecord(LoginCredentials lc)
	{
		try {
			PasswordGenerator passgen=new PasswordGenerator();
		
			lc.setPassword(passgen.generatePassword(lc.getStaffID()));
			lc.setSalt(passgen.getsalt());
			loginDAO.insertLoginCredentials(lc);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	
	}
	
	public boolean validate(String name,String password,String role){
		LoginCredentialsDAO lcDAOImpl=DAOFactory.loadInstance().getLoginCredentialsDAO();
		boolean status=false;
		try {
		LoginCredentials lc= lcDAOImpl.findLoginCredentials(name);
		if(lc!=null){
	    String salt=lc.getSalt();
	    String sPassword=password+salt;
	   String spHashcode=String.valueOf(sPassword.hashCode());
	   if(lc.getPassword().equals(spHashcode)&&lc.getRole().equals(role)&&lc.getStaffID().equals(name)){
		   
		  status=true;
	   }
		}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return status;
	}

	
	public void updateRecord(String staffid,String role){
	 	
		try {
			loginDAO.updateByID(staffid, role);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	
	}
	
	public String forgetPassword(String StaffID) throws StaffNotFoundException, SQLException{
		LoginCredentialsDAO lcDAOImpl=DAOFactory.loadInstance().getLoginCredentialsDAO();
		String SecurityQuestion=null;

			LoginCredentials lc=lcDAOImpl.findLoginCredentials(StaffID);
			if(lc==null){
				throw new StaffNotFoundException();
			}
			SecurityQuestion=lc.getSecurityQuestion();
			
			

		return SecurityQuestion;
	}

	
	public boolean checkSecurityAnswer(String StaffID,String SecurityAnswer){
		boolean status=false;
		LoginCredentialsDAO lcDAOImpl=DAOFactory.loadInstance().getLoginCredentialsDAO();

		try {
			LoginCredentials  lc = lcDAOImpl.findLoginCredentials(StaffID);
			String lcSecurityAnswer=lc.getSecurityAnswer();
			if(SecurityAnswer.equals(lcSecurityAnswer)){
				status=true;
			}
			

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return status;
		}

	
	public boolean checkPassword(String StaffID,String NewPassword,String ConfirmPassword){
		boolean status=false;
		if(NewPassword.equals(ConfirmPassword)){
			LoginCredentialsDAOImpl lcDAOImpl=new LoginCredentialsDAOImpl();
			Random rd=new Random();
			String Salt=String.valueOf(rd.nextInt(999));
			String snPassword=NewPassword+Salt;
			try {
				lcDAOImpl.updatePassword(StaffID, String.valueOf(snPassword.hashCode()), Salt);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			status=true;
		}
		return status;
	}

	
	}



